//
//  UpThemeVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/28.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "UpThemeVC.h"
#import "TiCaiCell.h"
@interface UpThemeVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionV;
@property (nonatomic, strong) NSMutableArray *selectArr;//选中的索引数组
@end

@implementation UpThemeVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor c2Color];
    [self setNav];
    [self loadData];
    [self loadCollectionV];
}

- (void)setNav
{
//    self.title = @"题材";
    self.title = NSLocalizedString(@"Subject", nil);
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 50, 50);
//    [right setTitle:@"确定" forState:UIControlStateNormal];
    [right setTitle:NSLocalizedString(@"Confirm", nil) forState:UIControlStateNormal];
    [right setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}

- (void)loadData
{
//    self.dataSource = @[@"人物", @"动物", @"植物", @"风景", @"静物", @"写实", @"抽象", @"超现实", @"建筑", @"花鸟", @"历史故事", @"宗教神话", @"节日祝福"];
    self.selectArr = [NSMutableArray array];
    for (int i = 0;  i < self.dataSource.count; i++)
    {
        [self.selectArr addObject:@(NO)];
    }
}

- (void)loadCollectionV
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, KSCREENWIDTH - 30, KSCREENHEIGHT) collectionViewLayout:flowLayout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerClass:[TiCaiCell class] forCellWithReuseIdentifier:@"ticai"];
    [self.collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
    [self.view addSubview:self.collectionV];
    self.collectionV.backgroundColor = [UIColor c2Color];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = self.dataSource[indexPath.row][@"title"];
    CGFloat width = [Tool widthForLabel:[NSString stringWithFormat:@"%@",str] fontSize:13];
    return CGSizeMake(width + 20, 30);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TiCaiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ticai" forIndexPath:indexPath];
    cell.textLabel.text = self.dataSource[indexPath.item][@"title"];
    cell.sel = [self.selectArr[indexPath.item] boolValue];
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        NSMutableArray *mutableArr = [NSMutableArray array];
        for (int i = 0; i < self.selectArr.count; i++)
        {
            NSNumber *sel = self.selectArr[i];
            if ([sel boolValue])
            {
                [mutableArr addObject:@(i)];
            }
        }
        BOOL str = [_selectArr[indexPath.item] boolValue];
        if (mutableArr.count >= 2 && !str)
        {
//            [self presentMessageTips:@"最多只能选择两个题材"];
            [self presentMessageTips:NSLocalizedString(@"Select no more than two subjects.", nil)];
        }
        else
        {
            [_selectArr removeObjectAtIndex:indexPath.item];
            [_selectArr insertObject:@(!str) atIndex:indexPath.item];
        }
        [collectionView reloadData];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KSCREENWIDTH, 50);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *resV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, resV.width, resV.height)];
//    label.text = @"请选择作品题材 (不超过2个)";
    label.text = NSLocalizedString(@"Choose subjects (no more than 2)", nil);
    label.textColor = [UIColor c4Color];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:15];
    [resV addSubview:label];
    return resV;
}

- (void)rightBtnClick
{
    NSString *str = @"";
    NSString *strId = @"";
    for (int i = 0; i < self.selectArr.count; i++)
    {
        NSNumber *sel = self.selectArr[i];
        if ([sel boolValue])
        {
            if ([str isEqualToString:@""])
            {
                str = self.dataSource[i][@"title"];
            }
            else
            {
                str = [str stringByAppendingString:[NSString stringWithFormat:@" %@", self.dataSource[i][@"title"]]];
            }
            if ([strId isEqualToString:@""])
            {
                strId = self.dataSource[i][@"id"];
            }
            else
            {
            strId = [strId stringByAppendingString:[NSString stringWithFormat:@",%@", self.dataSource[i][@"id"]]];
            }
        }
    }
    self.sureBlock([NSString stringWithFormat:@"%@", str], strId);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
